﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace ZCustomControl.Models
{
    public class ViewItemModel : INotifyPropertyChanged
    {
        #region Fields

        public event PropertyChangedEventHandler PropertyChanged;

        //节点名称
        private string _viewItemName = "";
        //节点id
        private string _id = "";
        //是否显示图标
        private bool _isShowIcon = true;
        //图标
        private string _icon = null;
        //是否显示图标背景
        private bool _isShowIconBackground = true;
        //图标背景
        private Brush _iconBackground = new SolidColorBrush(Colors.White);
        //是否选中
        private bool _isSelected = false;
        //是否显示选择控件
        private bool _isShowCheckControl = false;
        //(CheckBox或者RadioButton状态)是否选中
        private bool _isChecked = false;
        //是否展开子节点
        private bool _isExpanded = false;
        //父节点
        private ViewItemModel _parent = null;
        //附加信息
        private object _data = null;
        //附加排序字段
        private int _sortIndex = 0;
        //子节点
        private ObservableCollection<ViewItemModel> _children = new ObservableCollection<ViewItemModel>();

        #endregion

        #region Properties

        public string ViewItemName
        {
            get { return _viewItemName; }
            set
            {
                _viewItemName = value;
                RaisePropertyChanged("ViewItemName");
            }
        }

        public string Id
        {
            get { return _id; }
            set { _id = value; RaisePropertyChanged("Id"); }
        }
        
        public bool IsShowIcon
        {
            get { return _isShowIcon; }
            set { _isShowIcon = value; RaisePropertyChanged("IsShowIcon"); }
        }

        public string Icon
        {
            get { return _icon; }
            set { _icon = value; RaisePropertyChanged("Icon"); }
        }

        public bool IsShowIconBackground
        {
            get { return _isShowIconBackground; }
            set { _isShowIconBackground = value; RaisePropertyChanged("IsShowIconBackground"); }
        }

        public Brush IconBackground
        {
            get { return _iconBackground; }
            set { _iconBackground = value; RaisePropertyChanged("IconBackground"); }
        }

        public bool IsSelected
        {
            get { return _isSelected; }
            set { _isSelected = value; RaisePropertyChanged("IsSelected"); }
        }

        public bool IsChecked
        {
            get { return _isChecked; }
            set { _isChecked = value; RaisePropertyChanged("IsChecked"); }
        }

        public bool IsExpanded
        {
            get { return _isExpanded; }
            set { _isExpanded = value; RaisePropertyChanged("IsExpanded"); }
        }

        public bool IsShowCheckControl
        {
            get { return _isShowCheckControl; }
            set { _isShowCheckControl = value; RaisePropertyChanged("IsShowCheckControl"); }
        }

        public ViewItemModel Parent
        {
            get { return _parent; }
            set { _parent = value; RaisePropertyChanged("Parent"); }
        }

        public object Data
        {
            get { return _data; }
            set { _data = value; RaisePropertyChanged("Data"); }
        }

        public int SortIndex
        {
            get { return _sortIndex; }
            set { _sortIndex = value; RaisePropertyChanged("SortIndex"); }
        }

        public ObservableCollection<ViewItemModel> Children
        {
            get { return _children; }
            set { _children = value; RaisePropertyChanged("Children"); }
        }

        #endregion

        #region Constructors

        public ViewItemModel()
        {
        }

        #endregion

        #region Protected Methods

        protected void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region Public Methods

        public ViewItemModel Copy()
        {
            ViewItemModel copy = new ViewItemModel();
            copy.Icon = Icon;
            copy.Id = Id;
            copy.IsChecked = IsChecked;
            copy.IsExpanded = IsExpanded;
            copy.IsSelected = IsSelected;
            copy.IsShowCheckControl = IsShowCheckControl;
            copy.ViewItemName = ViewItemName;
            copy.Parent = Parent;
            copy.SortIndex = SortIndex;
            copy.Data = Data;

            //此方法子节点进行深度拷贝
            foreach (ViewItemModel vim in Children)
            {
                copy.Children.Add(vim.Copy());
            }

            return copy;
        }

        #endregion
    }
}
